
import { KeyGenerator } from "../../common/keygenerator";
class DataSaveSvc {
    constructor(dataManger) {
        this.dataManger = dataManger;
    }


    save(canvas, paintMap) {
        console.log("save data___________________");
        let data = this.getSaveData();
        let mapInfo = this.dataManger.getMapViewInfo();
        let mapSize = mapInfo.getMapSize();
        var image = paintMap.getImageData(0, 0, mapSize.width, mapSize.height);
        let imagedata = canvas.toDataURL("image/jpeg", 0.2);
        let extInfo = {backColor:mapInfo.backColor.toValue()};
        let result = { data, iconURL: imagedata,extInfo };
        return result;
    }

    getSaveData() {
        var graphFileObj = {};
        var array = [];
        var graphList = this.dataManger.getAllGraphs();

        for (var idx in graphList) {
            let graph = graphList[idx];
            if (graph.getUseful() != 0) {
                var info = graph.toInfo();
                if (info) {
                    var jsonoj = info.toJson();
                    array.push(jsonoj);
                }
            }
            else {
                console.log("delete graph");
                console.log(graph);
            }
        }
        graphFileObj["elements"] = array;

        var topodata = this.dataManger.getTopoManager().saveData();
        graphFileObj["topodata"] = topodata;

        let layArr = this.dataManger.getLayerManager().getSaveLayerJsonArr();
        graphFileObj["layers"] = layArr;

        if (array.length > 0)
        graphFileObj["exSymUnits"] = this.getExSymbolDatas();

        var version = "v0.0.0.1";
        graphFileObj["version"] = version;
        graphFileObj["mapScale"] = this.dataManger.getMapScale();
        graphFileObj["fileKind"] = 0;
        let mapViewInfo = this.dataManger.getMapViewInfo();
        let viewRect = mapViewInfo.getViewPort();
        let scaling = mapViewInfo.getScaling();
        let vleft = viewRect.left;
        let vtop = viewRect.top;
        let vright = viewRect.right;
        let vbottom = viewRect.bottom;
        graphFileObj["scaling"] = scaling;
        graphFileObj["vleft"] = vleft;
        graphFileObj["vtop"] = vtop;
        graphFileObj["vright"] = vright;
        graphFileObj["vbottom"] = vbottom;
        graphFileObj["seed"] = KeyGenerator.getSeed() + 1;
        graphFileObj["backColor"] = mapViewInfo.getBackColor().toValue();
        graphFileObj["fileMode"] = mapViewInfo.editMode;
        graphFileObj["projectScale"] = mapViewInfo.projectScale;
        return graphFileObj;
    }


    getExSymbolDatas() {
        var array = [];
        var symboList = this.dataManger.getSymbolManager().getAllSymbol();
        for (var m in symboList) {
            let symbol = symboList[m];
            if (symbol.exflag || symbol.getBeenRefFlag()) {
                var unitObjList = symbol.getGraphUnitLst();
                for (var n in unitObjList) {
                    let unitObj = unitObjList[n];
                    var info = unitObj.toInfo();
                    info.sSymbolName = symbol.m_name;
                    info.superID = symbol.uuid;
                    info.superSymType = symbol.getTypeID();
                    info.exflag = true;
                    var jsonoj = info.toJson();
                    array.push(jsonoj);
                }
            }
        }
        return array;
    }
}

export default { DataSaveSvc }